//
//  MusicPlayViewController.m
//  OurRelax
//
//  Created by 王雅蓉 on 15/6/18.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//


#define Tablebar 49
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#import "MusicPlayViewController.h"

#import "MusicPlayTableViewCell.h"

#import "DBCenter.h"
@interface MusicPlayViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)UIView *playView;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)UIImageView *love;
@property (nonatomic, retain)UIImageView *singMode;
@property (nonatomic, retain)UIImageView *pause;
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)NSMutableArray *arr;

@end

@implementation MusicPlayViewController
-(void)dealloc
{
    [_tableview release];
    [_playView release];
    [_love release];
    [_timer release];
    [_singMode release];
    [_pause release];
    [_scroll release];
    [_pageControl release];
    [_arr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self setUI];
    
}

-(void)setUI
{
    self.arr = [NSMutableArray array];
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    _scroll.bounces = NO;
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height - 64 - 100);
    //    self.scroll.contentOffset = CGPointMake(0, 64);
    [_scroll release];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.scroll addSubview:imageView];
    imageView.backgroundColor = [UIColor yellowColor];
    [imageView release];
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[MusicPlayTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.scroll addSubview:self.tableview];
    [_tableview release];
    self.playView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
    [self.view addSubview:self.playView];
    [_playView release];
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 10, 60, 10)];
    self.pageControl.numberOfPages = 3;
    //    pageControl.tag = 100;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithHue:0.9 saturation:0.9 brightness:0.9 alpha:0.5];
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self.playView addSubview:self.pageControl];
    self.playView.backgroundColor = [UIColor grayColor];
    [_pageControl release];
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 30, self.view.frame.size.width - 60, 10)];
#pragma mark 改变进度条图片
    //[slider setThumbImage:[UIImage imageNamed:@"jindu.png"] forState:UIControlStateNormal];
    //[slider setThumbImage:[UIImage imageNamed:@"jindu.png"] forState:UIControlStateHighlighted];
    
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.playView addSubview:slider];
    [slider release];
    self.singMode = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55, 30, 30)];
    self.singMode.userInteractionEnabled = YES;
    UITapGestureRecognizer *singModeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singModeTap)];
    [self.singMode addGestureRecognizer:singModeTap];
    [self.playView addSubview:self.singMode];
    self.singMode.image = [UIImage imageNamed:@"danqu.png"];
    [_singMode release];
    [singModeTap release];
    
    UIImageView *upSing = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 4, 55, 30, 30)];
    upSing.userInteractionEnabled = YES;
    UITapGestureRecognizer *upSingTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(upSingTap)];
    [upSing addGestureRecognizer:upSingTap];
    [self.playView addSubview:upSing];
    upSing.image = [UIImage imageNamed:@"up.png"];
    [upSing release];
    [upSingTap release];
    
    self.pause = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 20, 50, 40, 40)];
    self.pause.userInteractionEnabled = YES;
    UITapGestureRecognizer *pauseTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pauseTap)];
    [self.pause addGestureRecognizer:pauseTap];
    [self.playView addSubview:self.pause];
    self.pause.image = [UIImage imageNamed:@"play.png"];
    [_pause release];
    [pauseTap release];
    
    UIImageView *down = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 4 * 3 - 30, 55, 30, 30)];
    [self.playView addSubview:down];
    down.userInteractionEnabled = YES;
    UITapGestureRecognizer *downTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downTap)];
    [down addGestureRecognizer:downTap];
    down.image = [UIImage imageNamed:@"down.png"];
    [down release];
    [downTap release];
    
    self.love = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40, 60, 20, 20)];
    _love.image = [UIImage imageNamed:@"kong.png"];
    _love.userInteractionEnabled = YES;
    UITapGestureRecognizer *loveTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(loveTap)];
    [_love addGestureRecognizer:loveTap];
    [self.playView addSubview:_love];
    [_love release];
    [loveTap release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收起" style:UIBarButtonItemStylePlain target:self action:@selector(backButton:)];
}

-(void)backButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
    
        
    }];
}

#pragma mark 进度条方法
- (void)sliderAction:(UISlider *)slider
{
    NSLog(@"%f", slider.value);
}

#pragma mark 歌曲模式
- (void)singModeTap
{
    NSLog(@"单曲");
}

#pragma mark
- (void)pageAction:(UIPageControl *)pageAction
{
    //NSLog(@"%ld", self.pageControl.numberOfPages);
    _scroll.contentOffset = CGPointMake(self.view.frame.size.width * pageAction.currentPage, 0);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
}

#pragma mark 上一曲
- (void)upSingTap
{
    NSLog(@"up");
}

#pragma mark 下一曲
- (void)downTap
{
    NSLog(@"down");
}

#pragma mark 播放暂停
- (void)pauseTap
{
    NSLog(@"pause");
    self.pause.image = [UIImage imageNamed:@"pause.png"];
}

#pragma mark 点击收藏
- (void)loveTap
{
//    NSLog(@"love");
//    if (!_cell.isSelected) {
//        
//        _cell.isSelected = YES;
//        self.love.image = [UIImage imageNamed:@"shi.png"];
//    } else {
//    
//        _cell.isSelected = NO;
//        self.love.image = [UIImage imageNamed:@"kong.png"];
//    }
}

#pragma mark 隐藏tabBar
- (void)viewWillAppear:(BOOL)animated
{
    self.arr = [NSMutableArray array];
    self.tabBarController.tabBar.hidden = YES;
    DBCenter *dataBase = [DBCenter shareInstence];
    [dataBase openDB];
   NSMutableArray *array = [dataBase selectAll];

    for (NSInteger i = 0; i < [MusicDetailShareHandle shareListDataHandle].listShareArray.count; i++) {
        for (DataModel *song in array) {
            
            MusicForSonglist *temp = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:i];
            NSMutableArray *arrTemp = (NSMutableArray *)temp.url_list;
            NSString *strTemp = [[arrTemp objectAtIndex:0] objectForKey:@"url"];
            if ([song.url  isEqualToString:strTemp]) {
               
                
                [self.arr addObject:[NSString stringWithFormat:@"%ld", i]];
//                NSLog(@"+++++++++++++++");
            }
        }
        
        
        
    }
//    [MusicDetailShareHandle shareListDataHandle].listShareArray
    
   
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [MusicDetailShareHandle shareListDataHandle].listShareArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 155;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MusicPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
//    [self.cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartDefault.png"] forState:UIControlStateNormal];
    
  //初始化选中的单元格背景图片
    for (NSString *str in self.arr) {
        if (indexPath.row == [str integerValue]) {
            [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartSelected.png"] forState:UIControlStateNormal];
            cell.isSelected = YES;
            break;
        }
        else
        {
            [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartDefault.png"] forState:UIControlStateNormal];
            cell.isSelected = NO;
        }
    }
    
    //初始化背景图片
    if (cell.isSelected == YES) {
        
        [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartSelected.png"] forState:UIControlStateNormal];
        
    } else {
        
        [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartDefault.png"] forState:UIControlStateNormal];
        
    }
    
    //点击收藏改变
    [cell setBlock:^{
        
        NSLog(@">>>>>>点击");
        if (cell.isSelected == NO) {
        NSString *str = [NSString stringWithFormat:@"%ld", indexPath.row];
        [self.arr addObject:str];
            [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartSelected.png"] forState:UIControlStateNormal];
            NSLog(@"<<<<<<收藏");
            cell.isSelected = YES;
            DBCenter *dataBase = [DBCenter shareInstence];
            [dataBase openDB];
            [dataBase createTable];
            MusicForSonglist *temp = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:indexPath.row];
            NSMutableArray *arrTemp = (NSMutableArray *)temp.url_list;
           NSString *url = [[arrTemp objectAtIndex:0]objectForKey:@"url"];
            DataModel *model = [[DataModel alloc]init];
            model.url = url;
            model.name = temp.song_name;
            model.singer = temp.singer_name;
            [dataBase insertModel:model];

            
        }
        else if (cell.isSelected == YES)
        {
        NSString *temp = nil;
        for (NSString *strCom in self.arr) {
            if ([strCom integerValue] == indexPath.row) {
                temp = strCom;
            }
        }
        [self.arr removeObject:temp];
            [cell.SoCangBT setBackgroundImage:[UIImage imageNamed:@"musicCircleHeartDefault.png"] forState:UIControlStateNormal];
            NSLog(@"????????取消收藏");
        cell.isSelected = NO;
            DBCenter *dataBase = [DBCenter shareInstence];
            [dataBase openDB];
            
             MusicForSonglist *temp2 = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:indexPath.row];
            NSMutableArray *arrTemp = (NSMutableArray *)temp2.url_list;
            NSString *url = [[arrTemp objectAtIndex:0]objectForKey:@"url"];
            DataModel *model = [[DataModel alloc]init];
            model.url = url;
            [dataBase delete:model];
        
            
            
        }
        
       
    }];
    cell.playingSonglist = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:indexPath.row];
    
    return cell;
}
#pragma mark 选中某个单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
